package ru.nsu.fit.iconveyor.conveyor;

import ru.nsu.fit.iconveyor.conveyor.node.Node;

import java.util.*;

public class Conveyor {
    public static final String PROP_MAX_ITERATIONS_NUMBER = "maxIterationsNumber";

    private Map<String, Node> nodeMap = new HashMap<String, Node>();
    private Set<String> channelIds = new HashSet<String>();
    private Properties properties = new Properties();

    public Collection<Node> getNodes() {
        return Collections.unmodifiableCollection(nodeMap.values());
    }

    public Set<String> getChannelIds() {
        return Collections.unmodifiableSet(channelIds);
    }

    public void addNode(Node node) {
        if(node == null) {
            throw new IllegalArgumentException();
        }
        if(isIdExists(node.getId())) {
            throw new IllegalStateException("element with id " + node.getId() + " already exists in conveyor");
        }

        nodeMap.put(node.getId(), node);

    }

    public void addNodes(Node ... nodes) {
        for(Node node : nodes) {
            addNode(node);
        }
    }

    public Node getNodeById(String id) {
        return nodeMap.get(id);
    }

    public void addChannelId(String id) {
        if(isIdExists(id)) {
            throw new IllegalStateException("element with id " + id + " already exists in conveyor");
        }
        channelIds.add(id);
    }

    private boolean isIdExists(String id) {
        return nodeMap.containsKey(id) || channelIds.contains(id);
    }

    public Properties getProperties() {
        return properties;
    }

    public void setProperties(Properties properties) {
        this.properties = properties;
    }
}
